<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openSelectUser"
          >添加用户</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-circle-close" size="mini" :disabled="multiple"
          @click="cancelAuthUserAll" >批量取消授权</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-close" size="mini" @click="handleClose">关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange"
      :row-style="{ height: '35px' }" border :cell-style="{ padding: '0px' }">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号名称" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="用户姓名" prop="nickName" :show-overflow-tooltip="true" />
      <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true" />
      <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button v-if="dataScope == 6" size="mini" type="text" icon="el-icon-plus" @click="dataAuthUser(scope.row)"
            >指定数据授权</el-button>
          <el-button size="mini" type="text" icon="el-icon-circle-close" @click="cancelAuthUser(scope.row)"
           >取消授权</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <select-user ref="select" :roleId="queryParams.roleId" @ok="handleQuery" @selectUser="selectUser" :isActOrQues="isActOrQues" />
    <select-user ref="selects" :roleId="queryParams.roleId" :sign="sign" :userId="paramsUserId"
      @selectUser="selectUser" />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-button type="primary" icon="el-icon-plus" @click="showUser()">请选择用户</el-button>
        <el-input type="textarea" v-model="form.userName" disabled></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { allocatedUserList, authUserCancel, authUserCancelAll, getRole, selectDataAuthUser } from "@/api/system/role";
import { listView } from "@/api/system/view";
import selectUser from "./selectUser";

export default {
  name: "AuthUser",
  dicts: ['sys_normal_disable'],
  components: { selectUser },
  data() {
    return {
      isActOrQues:0,
      // 遮罩层
      loading: true,
      // 选中用户组
      userIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      sign: '',
      userNames: [],
      userIds: [],
      // 总条数
      total: 0,
      form: {
        userName: null,
      },
      dataScope: null,
      title: "",
      show: false,
      open: false,
      //文本域用户id
      paramsUserId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        roleId: undefined,
        userName: undefined,
        phonenumber: undefined
      }
    };
  },
  created() {
    const roleId = this.$route.params && this.$route.params.roleId;
    if(this.$store.state.user.info.userId!=1){
      this.isActOrQues=1
    }
    if (roleId) {
      this.queryParams.roleId = roleId;
      this.getList();
      this.getRole(roleId)
    }
  },
  methods: {
    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      allocatedUserList(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    getRole(roleId) {
      getRole(roleId).then(response => {
        this.dataScope = response.data.dataScope
      }
      );
    },
    dataAuthUser(row) {
      this.paramsUserId = row.userId
      this.showUser();
    },
    //
    selectUser(userIds, userNames) {
      this.show = false;
      this.sign = ''
      this.$modal.msgSuccess("保存成功");
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/role" };
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.userId)
      this.multiple = !selection.length
    },
    /** 打开指定数据授权 */
    showUser() {
      this.sign = "1"
      this.$nextTick(() => {
        this.$refs.selects.show();
      });

    },
    /** 打开授权用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show();

    },
    //确定
    submitForm() {
      this.open = false
      this.reset()
    },
    //取消
    cancel() {
      this.open = false
      this.reset()
    },
    //重置数据
    reset() {
      this.userIds = []
      this.userIds = []
      this.form.userNames = null
    },
    /** 取消授权按钮操作 */
    cancelAuthUser(row) {
      const roleId = this.queryParams.roleId;
      this.$modal.confirm('确认要取消该用户"' + row.userName + '"角色吗？').then(function () {
        return authUserCancel({ userId: row.userId, roleId: roleId });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消授权成功");
      }).catch(() => { });
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll(row) {
      const roleId = this.queryParams.roleId;
      const userIds = this.userIds.join(",");
      this.$modal.confirm('是否取消选中用户授权数据项？').then(function () {
        return authUserCancelAll({ roleId: roleId, userIds: userIds });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消授权成功");
      }).catch(() => { });
    }
  }
};
</script>
